using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Godot;

public partial class BaseLevelManager : Node2D
{
    async void CursorPressMoveCommand(Vector2I pos)
    {
        if (CurrentRobot.IsMoveEnd)
        {
            // 已移动过，无论点哪，都强制光标回到当前机器人
            Cursor.LookAt(CurrentRobot);
        }
        else
        {
            // 未移动过，且在移动范围内
            if (MapManager.IsPointInMoveRange(pos))
            {
                RobotManager.MoveRobot(pos, CurrentRobot, MapManager);
                await ToSignal(CurrentRobot, nameof(CurrentRobot.OnMoveEnd));
                // UIRobotInfo.RefreshControls();
            }
            else
            {
                // 不在移动范围内，取消移动指令
                // SetCurrentCommand(RobotUICommand.None);
                // MapManager.HideMoveRange();
                // UIRobotInfo.RefreshControls();
                Cursor.LookAt(CurrentRobot);
            }
        }
    }
    void CursorPressAttackCommand(Vector2I pos)
    {
        var target = GetCursorOnRobot();
        if (MapManager.IsPointInMoveRange(pos) && target != null)
        {
            if (CurrentWeapon.AllowTarget == SkillAllowTarget.Ally && !target.IsEnemy ||
                CurrentWeapon.AllowTarget == SkillAllowTarget.Enemy && target.IsEnemy)
            {
                UIRobotInfo.AnimateHide();
                MapManager.HideGrids();
                CurrentWeapon.Cast(new()
                {
                    Skill = CurrentWeapon,
                    Point = pos,
                    Target = GetCursorOnRobot()
                });
            }
            else
            {
                UILevelInfo.WarningMessage("目标无效");
                Cursor.LookAt(CurrentRobot);
            }
        }
        else
        {
            Cursor.LookAt(CurrentRobot);
        }
    }
    private void OnCursorPressedHandler(Vector2I pos)
    {
        var robot = GetCursorOnRobot();
        if (CurrentCommand == RobotUICommand.None)
        {
            if (robot != null && IsPlayerRonnd)
            {
                CurrentRobot = robot;
                UIRobotInfo.AnimateShow(robot);
            }
            else
            {
                UIRobotInfo.AnimateHide();
            }
        }
        else if (CurrentCommand == RobotUICommand.Move || CurrentCommand == RobotUICommand.UnLoad)
        {
            CursorPressMoveCommand(pos);
        }
        else if (CurrentCommand == RobotUICommand.Attack || CurrentCommand == RobotUICommand.Move)
        {
            CursorPressAttackCommand(pos);
        }
    }
    void EndTargetRobotAction()
    {
        SetCurrentCommand(RobotUICommand.None);
        CurrentRobot.SetRobotActionEnd();
        // UIRobotInfo.RefreshControls();
    }

    void RobotAttackCommand(RobotUICommand cmd)
    {
        SetCurrentCommand(cmd);
        UIRobotWeaponSelection.AnimateShow(CurrentRobot.Entity.WeaponList);
    }

    void RobotUndoCommand()
    {
        if (CurrentCommand == RobotUICommand.Attack || CurrentCommand == RobotUICommand.Skill)
        {
            MapManager.HideGrids();
            // UIRobotWeaponSelection.AnimateShow(CurrentRobot.Entity.WeaponList);
        }
        else if (CurrentCommand == RobotUICommand.Move)
        {
            CurrentRobot.UndoMove();
            MapManager.HideGrids();
            MapManager.UpdateMoveGridOnRobot(RobotManager);
            Cursor.LookAt(CurrentRobot);
        }
        SetCurrentCommand(RobotUICommand.None);
    }
    private void RobotCommandMove(RobotUICommand cmd)
    {
        SetCurrentCommand(cmd);
        MapManager.ShowGrids(CurrentRobot);
    }

    private void RobotLoadCommand()
    {
        SetCurrentCommand(RobotUICommand.None);
        var ms = RobotManager.GetMotherShip();
        ms.MotherShipLoadRobot(CurrentRobot);
        UIRobotInfo.AnimateHide();
    }

    private void OnRobotCommandHandler(string cmd)
    {
        var command = (RobotUICommand)Enum.Parse(typeof(RobotUICommand), cmd);
        if (command == RobotUICommand.Move)
        {
            RobotCommandMove(command);
        }
        else if (command == RobotUICommand.End)
        {
            UIConfirm.Confirm($"确定结束{CurrentRobot.Entity.RobotName}的行动吗？", endTargetRobotActionCallable);
        }
        else if (command == RobotUICommand.Attack)
        {
            RobotAttackCommand(command);
        }
        else if (command == RobotUICommand.Undo)
        {
            RobotUndoCommand();
        }
        else if (command == RobotUICommand.Detail)
        {
            UIRobotDetail.AnimateShow(CurrentRobot);
        }
        else if (command == RobotUICommand.Load)
        {
            RobotLoadCommand();
        }
        else if (command == RobotUICommand.UnLoad)
        {
            RobotUnLoadCommand();
        }
        else if (command == RobotUICommand.Skill)
        {
            UISkillSelection.AnimateShow(CurrentRobot);
        }
    }

    private void RobotUnLoadCommand()
    {
        SetCurrentCommand(RobotUICommand.UnLoad);
        UILevelInfo.ShowRobotList(CurrentRobot.LoadedRobotList, "请选择要起飞的机体");
    }


    private void OnRobotMoveEndHandler(Robot robot)
    {
        MapManager.UpdateMoveGridOnRobot(RobotManager);
    }

    void EndRoundHandler()
    {
        IsPlayerRonnd = false;
        UILevelInfo.AnimateHide();
        UIRobotInfo.AnimateHide();
        EndRound();
    }

    private void OnEnemyRoundEndHandler()
    {
        NextRound();
    }

    public void EndRound()
    {
        // 检测胜利/失败条件
        // TODO
        // AI回合
        RobotManager.RunEnemyAI();
    }

    private void OnMenuButtonCommandHandler(string command)
    {
        var cmd = Enum.Parse<LevelMenuCommnad>(command);
        if (cmd == LevelMenuCommnad.EndRound)
        {
            UIConfirm.Confirm("确定结束回合吗？", endRoundCallable);
        }
    }

    void RefreshUI()
    {
        // if (CurrentRobot != null)
        // {
        //     UIRobotInfo.RefreshControls();
        // }
        UILevelInfo.RefreshUI();
    }

    private void OnSelectWeaponHandler(WeaponEntity weapon)
    {
        CurrentWeapon = weapon;
        // 显示攻击范围
        MapManager.ShowSkillEffectRange(weapon);
    }

    private async void OnBattleEndHandler(Robot attacker, Robot target)
    {

        SetCurrentCommand(RobotUICommand.None);
        if (attacker.IsDeath)
        {
            if (CurrentRobot == attacker)
            {
                CurrentRobot = null;
                UIRobotInfo.AnimateHide();
            }
            await RobotManager.RobotDeath(attacker);
        }
        else
        {
            attacker.SetRobotActionEnd();
        }
        if (target.IsDeath)
        {
            if (CurrentRobot == target)
            {
                CurrentRobot = null;
                UIRobotInfo.AnimateHide();
            }
            await RobotManager.RobotDeath(target);
        }
        if (attacker.IsDeath || target.IsDeath)
        {
            MapManager.UpdateMoveGridOnRobot(RobotManager);
            UILevelInfo.RefreshUI();
        }
        if (CurrentRobot != null && (CurrentRobot.IsPlayer || CurrentRobot.IsAlly))
        {
            UIRobotInfo.AnimateShow(CurrentRobot);
        }
        CheckLevelSuccessOrFail();
    }

    private void OnVFXEndHandler()
    {
        if (CurrentCommand == RobotUICommand.Attack)
        {
            SetCurrentCommand(RobotUICommand.None);
            CurrentRobot.SetRobotActionEnd();
        }
    }


    private void OnRobotDestroyHandler(Robot robot)
    {
        // CheckLevelSuccessOrFail();
    }

    private void OnRobotListItemPressedHandler(Robot robot)
    {
        UILevelInfo.HideRobotList();
        if (CurrentCommand == RobotUICommand.None)
        {
            Cursor.LookAt(robot);
        }
        else if (CurrentCommand == RobotUICommand.UnLoad)
        {
            var pos = CurrentRobot.Position;
            CurrentRobot = robot;
            CurrentRobot.Position = pos;
            CurrentRobot.Show();
            MapManager.ShowGrids(robot);
            UIRobotInfo.AnimateShow(robot);
        }
    }

    private void OnStartBattleHandler()
    {
        UIRobotInfo.AnimateHide();
    }

    private void OnSkillItemSelectHandler(BaseSkill skill)
    {
        MapManager.ShowSkillEffectRange(skill);
    }
}